/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc;

import java.lang.reflect.Proxy;

/**
 * <p>Project: SpringPro - ProxyFactory2</p>
 * <p>Powered by yzc On 2023-10-19 19:47:46</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
public class ProxyFactory2 {


    public static <T> T createProxy(T target) {
        return (T) Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), target.getClass().getInterfaces(),
                (proxy, method, args) -> {
                    //增加横切逻辑的地方
                    //添加日志
                    System.out.println("开始订单日志");
                    //添加事务
                    System.out.println("开启事务");
                    //调用目标方法 target目标对象
                    Object obj = method.invoke(target, args);
                    //提交事务
                    System.out.println("提交事务");
                    System.out.println("保存订单成功");
                    return obj; //将执行目标方法的返回值 返回
                });
    }
}
